<?php
namespace fankers;

use fankers\lib\wx\Wx as WxBase;

class Wx
{
    public $_inst = null;

    public function index()
    {
        ## code=CODE&state=STATE 确认授权以后跳转到当前方法 带着code

        ## 生成code
        $wx = new WxBase();
        $wx->start();
        $code = $_GET['code'];
        $params = [
            'code' => $code,
        ];

        ##用code 获取token信息
        $class = WxBase::class;
        $result = $userInfo = [];
        if (is_callable([$class,'token'])) {

            if (!$this->_inst) {
                $this->_inst = new $class;
            }
            $result = $this->_inst->token($params);
        }


        ## 用返回的token信息 拉取用户信息
        if (!empty($result)) {
            $data = [
                'access_token' => $result['access_token'],
                'openid' => $result['openid'],
                'lang' => 'zh_CN'
            ];

            if (is_callable([$class,'info'])){
                if (!$this->_inst) {
                    $this->_inst = new $class;
                }

                $userInfo = $this->_inst->info($data);
            }
        }

        return $userInfo;
    }
}
